package br.com.dev.tcc.view.apolice;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.Apolice;
import br.com.dev.tcc.bean.DelegateListener;
import br.com.dev.tcc.bean.ResponseData;
import br.com.dev.tcc.bean.sinistro.Sinistro;
import br.com.dev.tcc.model.ApolicesModel;
import br.com.dev.tcc.model.SinistroModel;
import br.com.dev.tcc.task.BuscaSinistrosTask;
import br.com.dev.tcc.util.alarm.AlarmUtil;
import br.com.dev.tcc.view.DefaultSupportActionBarActivity;
import br.com.dev.tcc.view.dialog.ApolicesDialog;
import br.com.dev.tcc.view.dialog.ApolicesDialog.OnBuscaSinistroListener;
import br.com.dev.tcc.view.dialog.ApolicesDialog.OnRemoveListener;
import br.com.dev.tcc.view.dialog.ConfirmProfileDialog;
import br.com.dev.tcc.view.dialog.ConfirmProfileDialog.OnConfirmListener;
import br.com.dev.tcc.view.dialog.ErrorDialog;
import br.com.dev.tcc.view.dialog.ProgressCustomDialog;
import br.com.dev.tcc.view.dialog.SearchProfileDialog;
import br.com.dev.tcc.view.dialog.SearchProfileDialog.OnBuscaApoliceListener;

public class ApolicesActivity extends DefaultSupportActionBarActivity {

	private ApolicesModel model;
	private SinistroModel sinistroModel;
	private ApolicesView view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setActionBar(R.string.perfil, R.string.perfil_subtitulo,
				R.drawable.perfil);
		setContentView(R.layout.perfil);

		model = new ApolicesModel(this);
		view = new ApolicesView(this, new SearchClickListener(),
				new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> a, View v,
							int position, long id) {

						ApolicesDialog dialog = new ApolicesDialog(
								ApolicesActivity.this, model.get(position));
						dialog.setOnRemoveListener(removeListener);
						dialog.setOnBuscaListener(buscaListener);
						dialog.show();
					}
				});

		view.setLista(model.lista());
	}

	class SearchClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {

			SearchProfileDialog dialog = new SearchProfileDialog(
					ApolicesActivity.this);
			dialog.setListener(new BuscaApoliceListener());
			dialog.show();
		}
	}

	class BuscaApoliceListener implements OnBuscaApoliceListener {

		@Override
		public void onComplete(ResponseData<Apolice> response) {

			if (response.isSuccess()) {

				final Apolice apolice = response.getData();

				OnConfirmListener listener = new OnConfirmListener() {

					@Override
					public void onConfirm() {

						ProgressCustomDialog pd = new ProgressCustomDialog(
								ApolicesActivity.this);
						pd.setMessage(R.string.salvando_apolice);
						pd.setCancelable(false);
						pd.show();

						if (model.salva(apolice)) {

							setResult(RESULT_OK);
							
							Animation anim = new AlphaAnimation(1, 0);
							anim.setDuration(500);
							anim.setInterpolator(new AccelerateInterpolator());
							anim.setAnimationListener(new AnimationListener() {

								@Override
								public void onAnimationStart(Animation a) {
								}

								@Override
								public void onAnimationRepeat(Animation a) {
								}

								@Override
								public void onAnimationEnd(Animation a) {

									view.setLista(model.lista());
								}
							});

							view.getList().setVisibility(View.VISIBLE);
							view.getTvSemItens().setVisibility(View.GONE);
							view.getList().startAnimation(anim);
						} else
							Toast.makeText(ApolicesActivity.this,
									R.string.erro_salvar_apolice,
									Toast.LENGTH_LONG).show();

						pd.dismiss();
					}
				};

				ConfirmProfileDialog dialog = new ConfirmProfileDialog(
						ApolicesActivity.this, listener, apolice);
				dialog.setCancelable(false);
				dialog.show();
			} else {

				ErrorDialog dialog = new ErrorDialog(ApolicesActivity.this,
						response.getErrorMessage());
				dialog.show();
			}
		}
	}

	private OnRemoveListener removeListener = new OnRemoveListener() {
		@Override
		public void onRemove(Apolice apolice) {

			AlarmUtil util = new AlarmUtil(ApolicesActivity.this);
			util.cancelAlarm(apolice.getId());
			model.remove(apolice);

			Animation anim = new AlphaAnimation(1, 0);
			anim.setDuration(500);
			anim.setInterpolator(new AccelerateInterpolator());
			anim.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation a) {
				}

				@Override
				public void onAnimationRepeat(Animation a) {
				}

				@Override
				public void onAnimationEnd(Animation a) {

					view.setLista(model.lista());
				}
			});
			view.getList().startAnimation(anim);
		}
	};
	protected ProgressCustomDialog progressDialog;

	private OnBuscaSinistroListener buscaListener = new OnBuscaSinistroListener() {
		@Override
		public void onBusca(Apolice apolice) {
			progressDialog = new ProgressCustomDialog(
					ApolicesActivity.this);
			progressDialog.setMessage(R.string.buscando_sinistros);
			progressDialog.show();

			new BuscaSinistrosTask(ApolicesActivity.this, delegate, apolice)
					.execute();
		}
	};

	private DelegateListener<ResponseData<List<Sinistro>>> delegate = new DelegateListener<ResponseData<List<Sinistro>>>() {
		@Override
		public void onDelegate(ResponseData<List<Sinistro>> data) {
			if(data.isSuccess()){
				progressDialog.setMessage(R.string.salvando_sinistros);
				sinistroModel = new SinistroModel(ApolicesActivity.this);
				for(Sinistro sinistro : data.getData())
					sinistroModel.salva(sinistro);
				Toast.makeText(ApolicesActivity.this, R.string.sinistros_salvos, 1).show();
			}
			else
				Toast.makeText(ApolicesActivity.this, data.getErrorMessage(), 1).show();
			progressDialog.dismiss();
		}
	};
}
